package gitobject;

import java.io.*;

public class Head extends GitObject implements Serializable {

    private static String branchName = "master";
    public static String headPath = repository.Repository.getGitDir() + File.separator + "HEAD";

    public Head(String hash) throws Exception {

        this.key = hash;
        this.writeObject();
    }

    public void setKey(String key) {
        this.key = key;

    }

    public String getBranchName() {
        return this.branchName;
    }

    public static String setName(String name) {
        branchName = name;
        return branchName;
    }

    public void writeObject() throws Exception {
        File file = new File(headPath);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
        objectOutputStream.writeObject(this);
    }

    public static Head deserialize() throws IOException, ClassNotFoundException {

        File file = new File(headPath);
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file));
        Head head = (Head)objectInputStream.readObject();
        return head;
    }
}
